﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ParseXAML;
using System.Xml;

namespace ParseXAMLTest
{
    /// <summary>
    /// Summary description for ParseXAMLTest
    /// </summary>
    [TestClass]
    public class ParseXAMLTest
    {
        private string dirBase = @"G:\Codeplex\SLLocalize\ParseXAMLTest\";

        public ParseXAMLTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        public string ReadFile(string fileName)
        {
            using (System.IO.StreamReader fs = new System.IO.StreamReader(fileName))
            {
                return fs.ReadToEnd();
            }
        }

        public void Compare(string dirGen, string dirTest, string fileName, string extension)
        {
            Assert.AreEqual(ReadFile(dirGen + fileName), ReadFile(dirTest + fileName));
            Assert.AreEqual(ReadFile(dirGen + XAMLFile.DirResources + "\\" + fileName + ".resx"), ReadFile(dirTest + XAMLFile.DirResources + "\\" + fileName + ".resx"));
            Assert.AreEqual(ReadFile(dirGen + fileName + ".translat" + extension), ReadFile(dirTest + fileName + ".translat" + extension));
        }

        public void Test(bool isByCode, bool genID, string dirSource, string fileName, string dirCompare, string extension)
        {
            Global global = new Global();
            global.DefaultNamespace = dirSource;
            global.IsSilverlight = false;
            //            string dirBase = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().CodeBase );
            //            dirBase = System.IO.Path.GetDirectoryName( dirBase );
            string dirTest = System.IO.Path.Combine(dirBase, @"Compare\"+dirCompare+@"\");
            string dirGen = System.IO.Path.Combine(dirBase, @"Temp\" + dirCompare + @"\");
            string dirXAML = System.IO.Path.GetFullPath(System.IO.Path.Combine(dirBase, @"..\"+dirSource+@"\"));
            global.Folderpath = dirGen;
            global.IsGenerateCodeBehind = isByCode;
            if ( isByCode )
                global.CreateNameAttribute = genID;
            else
                global.CreateUidAttribute = genID;

            // clean first, some resX file can be incremental !
            if (System.IO.Directory.Exists(dirGen))
            {
                System.IO.Directory.Delete(dirGen, true);
            }
            if (!System.IO.Directory.Exists(dirGen))
            {
                System.IO.Directory.CreateDirectory(dirGen);
            }

            System.IO.File.Copy(System.IO.Path.Combine(dirXAML, fileName), System.IO.Path.Combine(dirGen, fileName), true);
            XAMLFile x = new XAMLFile(global, extension);
            Outputs o = x.Go(System.IO.Path.Combine(dirGen, fileName));
            o.Save();

            Compare(dirGen, dirTest, fileName, extension);
        }

        [TestMethod]
        public void TestWPFCSharpByCode()
        {
            Test(true, false, "WPFTestC", "Window1.xaml", "WPFCCode", ".cs");
        }

        [TestMethod]
        public void TestWPFCSharpByBinding()
        {
            Test(false, false, "WPFTestC", "Window1.xaml", "WPFCBinding", ".cs");
        }

        [TestMethod]
        public void TestWPFCSharpUidByBinding()
        {
            Test(false, true, "WPFTestC", "Window1.xaml", "WPFCUidBinding", ".cs");
        }

        [TestMethod]
        public void TestWPFCSharpNameByCode()
        {
            Test(true, true, "WPFTestC", "Window1.xaml", "WPFCNameCode", ".cs");
        }

        [TestMethod]
        public void TestWPFVBSharpByCode()
        {
            Test(true, false, "WPFTestC", "Window1.xaml", "WPFVBCode", ".vb");
        }

        [TestMethod]
        public void TestWPFVBSharpByBinding()
        {
            Test(false, false, "WPFTestC", "Window1.xaml", "WPFVBBinding", ".vb");
        }
    }
}
